﻿#region CREDIT & LICENSE
/*******************************************
 * CREDIT: 
 * http://www.ROMPROS.com 
 * Tremendously helpful website, also would
 * like to give special thanks to:
 * 
 * Roverser - for the great effort on a c repository 
 * that most of this was ported from. http://code.google.com/p/rombot/
 * 
 * ROMeo - for the great amount of information shared
 * all across the rompros site. http://www.rompros.com/member.php?u=1901
 * 
 * 
 * LICENSE:
 *  
 * RomUI is WPF(Windows Presentation Foundation) user interface for interacting with the 
 * RomAPI used to manipulate the Runes Of Magic game client. 
 * 
 * Copyright (C) 2010  digitalxarts@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *******************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using RomAPI.Objects;
using RomAPI;
using System.Collections;
using System.Timers;
namespace RomUI
{
    /// <summary>
    /// Interaction logic for RadarUnit.xaml
    /// </summary>
    public partial class RadarUnit : UserControl
    {
        public RomUnit Unit;
        public int UnitIndex;
        private Timer timeoutTimer;
        private int timeoutFrequency;
        public RadarUnit(int id, RomUnit unit)
        {

            InitializeComponent();
            UnitIndex = id;
            Unit = unit;
            timeoutFrequency = 300; //1 sec
            
            timeoutTimer = new Timer(timeoutFrequency);
            timeoutTimer.Elapsed += new ElapsedEventHandler(timeoutTimer_Elapsed);
            timeoutTimer.Start();
        }


        public delegate void TimeoutHandler();
        public event TimeoutHandler OnTimeout;
        private void Timeout()
        {
            this.Visibility = System.Windows.Visibility.Hidden;
            
            if(OnTimeout != null)
                OnTimeout();
        }
        void timeoutTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new TimeoutHandler(Timeout));
        }
        private delegate void RefreshHandler();
        private void Refresh() {
            try
            {
                timeoutTimer.Stop();
                this.Visibility = System.Windows.Visibility.Visible;
                Unit.Refresh();
                RomPlayer Player = Unit.Client.Player;
                if (Unit.BaseAddress > 100 && Player.BaseAddress != Unit.BaseAddress)
                {
                    float x = -(Player.X - Unit.X) * (float)(Radar.RadarScale) + (float)(Radar.RadarWidth / 2 + 135);
                    float y = (Player.Y - Unit.Y) * (float)(Radar.RadarScale) + (float)(Radar.RadarHeight);
                    this.Margin = new Thickness((int)x, (int)y, 0, 0);
                    if (Radar.FilterName == null || Radar.FilterName == Unit.Name)
                    {
                        switch (Unit.Type)
                        {
                            case RomUnitType.UNIT:
                                elRadarShape.Fill = Brushes.Red;
                                break;
                            case RomUnitType.RESOURCE:
                                elRadarShape.Fill = Brushes.Green;
                                break;
                            case RomUnitType.PLAYER:
                                elRadarShape.Fill = Brushes.Blue;
                                break;
                            default:
                                elRadarShape.Fill = Brushes.DarkMagenta;
                                break;
                        }
                    }
                    else
                    {
                        elRadarShape.Fill = Brushes.Gray;
                    }
                    if (x < 0 || y < 0)
                        this.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Hidden;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Rom Must be Running");
                throw e;
                Console.WriteLine(e.Message);
            }
            timeoutTimer.Start();
        }
        public void Update(RomWorld world)
        {
            
            //we don't want to load something that doesn't exist... lets make sure we aren't updating a null reference.
            if(world.CurrentSize > UnitIndex)
                this.Dispatcher.Invoke(new RefreshHandler(Refresh));
        }
    }
}
